package guowei.guliproduct.product.service.impl;

import guowei.guliproduct.product.config.MyselfThreadPoolConfig;
import guowei.guliproduct.product.entity.ProductAttrValueEntity;
import guowei.guliproduct.product.entity.SkuImagesEntity;
import guowei.guliproduct.product.entity.SpuInfoDescEntity;
import guowei.guliproduct.product.service.*;
import guowei.guliproduct.product.vo.SkuItemSaleAttrVo;
import guowei.guliproduct.product.vo.SkuItemVo;
import guowei.guliproduct.product.vo.SpuItemAttrGroupVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import guowei.common.utils.PageUtils;
import guowei.common.utils.Query;

import guowei.guliproduct.product.dao.SkuInfoDao;
import guowei.guliproduct.product.entity.SkuInfoEntity;
import org.springframework.util.StringUtils;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {

    @Autowired
    SkuImagesService skuImagesService;

    @Autowired
    private SpuInfoDescService spuInfoDescService;

    @Autowired
    private SkuSaleAttrValueService skuSaleAttrValueService;

    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private ThreadPoolExecutor executor;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                new QueryWrapper<SkuInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageByKeys(Map<String, Object> params) {

        String key = (String) params.get("key");
        String catalogId = (String) params.get("catalogId");
        String brandId = (String) params.get("brandId");
        String min = (String) params.get("min");
        String max = (String) params.get("max");

        QueryWrapper<SkuInfoEntity> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(catalogId)){
            wrapper.eq("catalog_id",catalogId);
        }
        if (!StringUtils.isEmpty(brandId)){
            wrapper.eq("brand_id",brandId);
        }
        if (!StringUtils.isEmpty(max) && !"0".equals(max)){
            wrapper.le("price",max);
        }
        if (!StringUtils.isEmpty(min) && !"0".equals(min)){
            wrapper.ge("price",max);
        }
        if (!StringUtils.isEmpty(key)){
            wrapper.and(one->{
                one.eq("sku_id",key).or().like("sku_name",key).or().eq("price",key);
            });
        }

        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                wrapper
        );
        return new PageUtils(page);
    }

    /**
     *  查到商品点击后的详细信息   利用线程池技术实现  异步编排
     * @param skuId
     * @return
     */
    @Override
    public SkuItemVo itemInfo(Long skuId) throws ExecutionException, InterruptedException {
        SkuItemVo skuItemVo = new SkuItemVo();

//    1. sku基本信息获取  pms_sku_info
        CompletableFuture<SkuInfoEntity> info = CompletableFuture.supplyAsync(() -> {
            SkuInfoEntity skuInfo = this.getOne(new QueryWrapper<SkuInfoEntity>().eq("sku_id", skuId));
            skuItemVo.setInfo(skuInfo);
            return skuInfo;
        }, executor);

//    3.  获取spu的销售属性组合
        CompletableFuture<Void> spuSale = info.thenAcceptAsync(res -> {
            List<SkuItemSaleAttrVo> skuItemSaleAttrVo = skuSaleAttrValueService.getValuesBuSpuId(res.getSpuId());
            skuItemVo.setSaleAttr(skuItemSaleAttrVo);
        }, executor);

//    4.  获取spu的介绍
        CompletableFuture<Void> spuDesc = info.thenAcceptAsync(res -> {
            SpuInfoDescEntity spuInfoDescEntity = spuInfoDescService.getOne(new QueryWrapper<SpuInfoDescEntity>().eq("spu_id", res.getSpuId()));
            skuItemVo.setDesp(spuInfoDescEntity);
        }, executor);

//    5.  获取spu的规格参数信息
        CompletableFuture<Void> spuGroup = info.thenAcceptAsync(res -> {
            List<SpuItemAttrGroupVo> groupAttrs = attrGroupService.getThreeBuSpuId(res.getCatalogId(), res.getSpuId());
            skuItemVo.setGroupAttrs(groupAttrs);
        }, executor);

//    2. sku图片信息  pms_sku_images   此方法不需要异步编排
        CompletableFuture<Void> spuImg = CompletableFuture.runAsync(() -> {
            List<SkuImagesEntity> skuImagesEntities = skuImagesService.list(new QueryWrapper<SkuImagesEntity>().eq("sku_id", skuId));
            skuItemVo.setImages(skuImagesEntities);
        }, executor);

//  等待所有异步任务完成，才下一步
        CompletableFuture.allOf(spuSale,spuDesc,spuGroup,spuImg).get();
        return skuItemVo;
    }

    @Override
    public BigDecimal getPriceBuSkuId(Long skuId) {
        SkuInfoEntity skuInfoEntity = this.getOne(new QueryWrapper<SkuInfoEntity>().eq("sku_id", skuId));
        return skuInfoEntity.getPrice();
    }

}